package com.itheima.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.mapper.SmsHomeAdvertiseMapper;
import com.itheima.pojo.SmsCouponHistory;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【sms_home_advertise(首页轮播广告表)】的数据库操作Service实现
 * @createDate 2022-10-11 14:26:17
 */
@DubboService
public class SmsHomeAdvertiseApiImpl implements SmsHomeAdvertiseApi {

    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;

    /**
     * 添加广告
     *
     * @param advertise
     */
    public void saveAdvertise(SmsHomeAdvertise advertise) {
        advertiseMapper.insert(advertise);
    }

    /**
     * 分页查询广告列表
     *
     * @param advertise
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResult findAdvertise(SmsHomeAdvertise advertise, Integer pageNum, Integer pageSize) {
        //构造查询条件
        //分页构造器
        IPage<SmsHomeAdvertise> iPage = new Page<>(pageNum, pageSize);
        //条件构造器
        LambdaQueryWrapper<SmsHomeAdvertise> lqw = new LambdaQueryWrapper<>();
        //条件判断
        if (ObjectUtil.isNotNull(advertise)) {
            lqw.like(ObjectUtil.isNotNull(advertise.getName()), SmsHomeAdvertise::getName, advertise.getName())
                    .eq(advertise.getType() != null, SmsHomeAdvertise::getType, advertise.getType())
                    .le(advertise.getEndTime() != null, SmsHomeAdvertise::getEndTime, advertise.getEndTime())
                    .orderByDesc(SmsHomeAdvertise::getSort);//排序
        }
        //System.out.println(advertise.getEndTime());
        advertiseMapper.selectPage(iPage, lqw);
        List<SmsHomeAdvertise> list = iPage.getRecords();
        Integer total = Convert.toInt(iPage.getTotal());//总条数
        Integer totalPage = Convert.toInt(iPage.getPages());//当前分页总页数
        return new PageResult(pageNum, pageSize, total, totalPage, list);

    }

    /**
     * 广告删除
     *
     * @param ids
     * @return
     */
    public Boolean deleteAdvertise(List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<SmsHomeAdvertise> lqw = new LambdaQueryWrapper<>();
        lqw.in(SmsHomeAdvertise::getId, ids);
        int delete = advertiseMapper.delete(lqw);
        return delete > 0;
    }

    /**
     * 修改上下线状态
     */
    public void updateStatus(SmsHomeAdvertise advertise) {

        advertiseMapper.updateById(advertise);

    }

    /**
     * 根据id查询广告
     *
     * @param id
     * @return
     */
    public SmsHomeAdvertise findAdvertiseById(Long id) {
        return advertiseMapper.selectById(id);
    }
}




